package com.idanchuang.component.base.actuator;

import com.idanchuang.component.base.config.BaseConfigBean;
import com.idanchuang.component.core.ServiceInfo;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yjy
 * Created at 2021/3/24 3:08 下午
 */
public class DcInfoEndpoint implements InfoContributor {

    private static final String ENABLED_AGENT_PROPERTY = "ENABLED_AGENT_DC";
    public static Map<String, String> COMPONENT_VERSION = null;
    public static String ENV = null;
    private final String startTime;
    private final BaseConfigBean baseConfigBean;

    private final static String HOSTNAME = System.getenv("HOSTNAME");
    private final static String DEPLOYMENT_NAME = System.getenv("PAAS_APP_NAME");
    private final static String NAMESPACE = System.getenv("PAAS_NAMESPACE");

    public DcInfoEndpoint(BaseConfigBean baseConfigBean) {
        this.baseConfigBean = baseConfigBean;
        this.startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
        DcInfoEndpoint.ENV = baseConfigBean.getEnv();
    }

    @Override
    public void contribute(Info.Builder builder) {
        Map<String, Object> map = new HashMap<>();
        map.put("versions", getComponentVersions());
        map.put("env", this.baseConfigBean.getEnv());
        map.put("appId", ServiceInfo.getAppId());
        map.put("hostname", HOSTNAME);
        map.put("deploymentName", DEPLOYMENT_NAME);
        map.put("namespace", NAMESPACE);
        map.put("address", ServiceInfo.address());
        map.put("agent", getEnabledAgent());
        map.put("version", ServiceInfo.getVersion());
        map.put("group", ServiceInfo.getGroup());
        map.put("startTime", this.startTime);
        builder.withDetail("appInfo", map);
    }

    /**
     * @return 启用的探针
     */
    public static String getEnabledAgent() {
        // 由 融合探针库 在启动时设值
        return System.getProperty(ENABLED_AGENT_PROPERTY);
    }

    public String getStartTime() {
        return startTime;
    }

    public BaseConfigBean getBaseConfigBean() {
        return baseConfigBean;
    }

    private static final String VERSION_RESOURCE_LOCATION = "META-INF/acg-version.properties";

    public static String getComponentVersion() {
        return getComponentVersion("component-base");
    }

    public static String getComponentVersion(String name) {
        return getComponentVersions().get(name);
    }

    public synchronized static Map<String, String> getComponentVersions() {
        if (COMPONENT_VERSION == null) {
            HashMap<String, String> versions = new HashMap<>();
            try {
                Enumeration<URL> urls = DcInfoEndpoint.class.getClassLoader().getResources(VERSION_RESOURCE_LOCATION);
                while (urls.hasMoreElements()) {
                    URL url = urls.nextElement();
                    UrlResource resource = new UrlResource(url);
                    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                    for (Map.Entry<?, ?> entry : properties.entrySet()) {
                        String name = ((String) entry.getKey()).trim();
                        String value = ((String) entry.getValue()).trim();
                        versions.put(name, value);
                    }
                }
            } catch (IOException ex) {
                throw new IllegalArgumentException("Unable to load versions from location [" +
                        VERSION_RESOURCE_LOCATION + "]", ex);
            }
            COMPONENT_VERSION = versions;
        }
        return COMPONENT_VERSION;
    }

}
